package com.ruoyi.web.studentmanagementsystem.tool;

import com.ruoyi.web.studentmanagementsystem.mapper.SysCodeMapper;
import com.ruoyi.web.studentmanagementsystem.pojo.SysCode;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: Captcha
 * Package: com.ruoyi.web.studentmanagementsystem.tool
 * Description:生成验证码
 *
 * @Author Mr. o
 * @Create 2023/11/16 17:16
 * @Version 1.0
 */
@Data
@Configuration
public class Captcha {

    @Resource
    private RedisTemplate<Object, Object> redisTemplate;

    @Resource
    private SysCodeMapper sysCodeMapper;

    @Resource
    @Autowired
    private SysCode sysCode;

    /**
     * 更新验证码模板
     *
     * @param
     * @return
     */
    public void updateCode() {
        for (int i = 1; i < 11; i++) {

            // 定义随机数对象
            Random random = new Random();

            // 获取随机值:1-10
            int i1 = random.nextInt(10) + 1;
            int i2 = random.nextInt(10) + 1;

            // 定义运算符数组
            char[] operators = {'+', '-', '*', '/'};
            // 根据数组获取随机下标
            int index = random.nextInt(operators.length);
            // 根据下标获取运算符
            String operator = operators[index] + "";

            // 定义结果
            int result = 0;

            // 判定运算符
            switch (operator) {
                case "+":
                    result = i1 + i2;
                    break;
                case "-":
                    result = i1 - i2;
                    break;
                case "*":
                    result = i1 * i2;
                    break;
                case "/":
                    result = i1 / i2;
                    break;
            }

            // 构建表达式
            String expression = i1 + " " + operator + " " + i2 + " = " + " ?";

            // 存储结果
            sysCodeMapper.updateCode(Integer.toString(i), expression, Integer.toString(result));
        }
    }

    /**
     * 生成表达式验证码
     *
     * @param
     * @return 表达式
     */
    public String getCode() {
        
        // 定义随机数对象
        Random random = new Random();

        // 获取随机值:1-10
        String i = Integer.toString(random.nextInt(10) + 1);

        List<SysCode> list = sysCodeMapper.selectCode();

        //随机一个验证码模板
        for (SysCode l : list) {
            if (l.getCodeId().equals(i)) {
                //设置验证码答案有效期：3分钟
                redisTemplate.opsForValue().set("answer", l.getCodeAnswer(),3, TimeUnit.MINUTES);
                return l.getCodeExpression();
            }
        }
        return "";
    }

    /**
     * 验证验证码
     *
     * @param
     * @return 验证状态
     */
    public String selectCode(String answer) {
        //判断验证码是否有效
        if(redisTemplate.opsForValue().get("answer") != null) {
            //判断验证码是否正确
            if(answer.equals(redisTemplate.opsForValue().get("answer"))) {
                return "验证成功";
            }else {
                return "验证失败";
            }
        }else {
            return "验证码已失效";
        }
    }

    /**
     * 用户在线状态
     *
     * @param
     * @return 在线状态
     */
    public String idSelect() {
        //判断用户是否在线
        if(redisTemplate.opsForValue().get("studentId") != null) {
            //重置用户在线状态
            redisTemplate.opsForValue().set("studentId", redisTemplate.opsForValue().get("studentId"), 30, TimeUnit.MINUTES);
            return "欢迎使用学生管理系统";
        } else {
            return "登录超时";
        }
    }
}

